/********************************************************************
	created:	2011/9/5
	filename: 	Character.cpp
	author:		Mwolf
	
	purpose:	
*********************************************************************/
#include "Character.h"
#include "BoundBox.h"
#include "GameStage.h"
#include "GameMain.h"
#include "Renderer/TextureManager.h"
#include <assert.h>

// define size of a character
float Player::m_sCharSize = 0.8f;
ResourceStateEnum Player::m_sResourceState = RES_NOT_LOADED;

#define PLAYER_SPRITE_RESOURCE			"Data/Person.tga"

Player* CreatePlayer()
{
	Player* player = new Player;
	AddActorToGame(player);

	return player;
}

Patient* CreatePatient()
{
	Patient* patient = new Patient;
	AddActorToGame(patient);

	return patient;
}

Player::Player()
: m_CanJump(false),
  m_ClimbingLadder(false),
  m_FacingLeft(false),
  m_SkeletonDisplay(true)
{
	m_Bound = BoundBox(-0.5f * m_sCharSize, -0.5f * m_sCharSize,
						0.5f * m_sCharSize, 0.5f * m_sCharSize);

	LoadResources();
	m_PlayerSkeleton = CreateSkeleton2D();
	m_PlayerSkeleton->LoadSkeleton2D();
}

void Player::Render()
{
	Vector2 vMin = Vector2(-0.5f, -0.5f) * m_sCharSize,
			vMax = Vector2(0.5f, 0.5f) * m_sCharSize * 2;

	//RenderSystem::DrawColoredSprite(vMin, vMax, GWColor(1.0f, 0.949f, 0.0f), (float)m_Layer);

	const TEXTURE_INFO* TextureInfo = TextureManager::Instance().GetTextureByName( PLAYER_SPRITE_RESOURCE );
	assert( TextureInfo );

	//RenderSystem::LoadIdentityModelMatrix();
	vMin += GetPositionWithCameraTilt();
	vMax += GetPositionWithCameraTilt();

	//RenderSystem::DrawSprite(vMin, vMax, TextureInfo, 0.f);

	RenderSystem::DrawSpriteEX(vMin, vMax, TextureInfo, 0.f, m_FacingLeft, 0);

	m_PlayerSkeleton->Render();
}

void Player::Update()
{
	//static int iSaveCount = 0;
	//static Vector2 bufferPosition = GetPosition();
	m_LastSafePos = Vector2(-4.5f, 3.5f);

	if ( GetPosition().y < -20.0f)
	{
		m_PlayerState = PLATER_STATE_DEAD;
//		iSaveCount = 0;
		SetPosition(m_LastSafePos);
	}

	//if (m_PlayerState == PLATER_STATE_IDLE)
	//{
	//	iSaveCount++;
	//	if ( iSaveCount >= 300)
	//	{
	//		m_LastSafePos = bufferPosition;
	//		bufferPosition = GetPosition();
	//		iSaveCount = 0;
	//	}
	//}

	if (m_ClimbingLadder)
	{
		m_Velocity = Vector2::ZERO;
	}
	else
	{
		// Base class update
		Actor::Update();
	}

	m_CanJump = false;
}

float Player::GetSize() const
{
	return m_sCharSize;
}

void Player::Jump()
{
	if (m_ClimbingLadder)
	{
		m_ClimbingLadder = false;
	}
	else if (m_CanJump)
	{
		m_Velocity.y = 0.35f;
		m_PlayerState = PLATER_STATE_JUMP;
	}
}

void Player::OnHitGround( GW_INT8 depth )
{
	Actor::OnHitGround(depth);

	m_CanJump = true;
	m_PlayerState = PLATER_STATE_IDLE;
}

void Player::LoadResources()
{
	if (m_sResourceState != RES_NOT_LOADED)
		return;

	if ( TextureManager::Instance().LoadTextureFromFile( PLAYER_SPRITE_RESOURCE ) )
		m_sResourceState = RES_LOADED;
	else
		m_sResourceState = RES_INVALID_RESOURCE;
}

Patient::Patient()
{
	m_Bound = BoundBox(-0.45f, -0.45f, 0.45f, 0.45f);
}

void Patient::Render()
{
	Vector2 vMax = Vector2(0.45f, 0.45f),
			vMin = -vMax;

	vMin += GetPositionWithCameraTilt();
	vMax += GetPositionWithCameraTilt();

	RenderSystem::DrawColoredSprite(vMin, vMax, GWColor(0.0f, 1.0f, 0.949f), 0.f);
}

float Patient::GetSize() const
{
	return .9f;
}

void Patient::Interactive()
{
}
